package com.sport.facilities.booking.system.htmlchecker;

import java.util.ArrayList;
import java.util.List;

import org.htmlparser.Node;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;

import android.util.Log;

import com.sport.facilities.booking.system.core.HtmlHelper;
import com.sport.facilities.booking.system.model.Booking;

public class InquireConfirmedPageChecker extends BasicChecker {
	private final String TAG = "InquireConfirmPageChecker";

	public List<Booking> getConfirmListfromHtml(String confirmedbookHtml)
			throws ParserException, Exception {
		
		super.hasError(confirmedbookHtml);
		// Get NodeList from HTML
		List<Booking> bookingList = new ArrayList<Booking>();
		NodeList nodelist = HtmlHelper.getNodeListByTag(confirmedbookHtml,
				"table");
		
		TagNameFilter fontFilter = new TagNameFilter("FONT");
		HasAttributeFilter hasSizeAttribute = new HasAttributeFilter("size");
		AndFilter testFilter = new AndFilter(fontFilter, hasSizeAttribute);
		NodeList foundNodeList = nodelist.extractAllNodesThatMatch(testFilter,
				true);
		// Log.d(TAG, "TextNode:"+foundNodeList.size());

		if (foundNodeList.size() % 6 != 0) {
			/*
			 * We have five elements, so the record must should be divided by 5
			 * [Date, Time, Centre, Court, Activity]
			 */
			throw new Exception("Data elements miss match!");
		}

		/*
		 * The first five textNode are the Header We should skip the first 5
		 * results Result should be as follow : Total no. of node found : 10 Txt
		 * (897[21,54],901[21,58]): Date Txt (1007[24,54],1011[24,58]): Time Txt
		 * (1117[27,54],1123[27,60]): Centre Txt (1229[30,54],1234[30,59]):
		 * Court Txt (1340[33,54],1348[33,62]): Activity Txt
		 * (1482[38,54],1493[38,65]): 12-Jan-2013 Txt (1599[41,54],1610[41,65]):
		 * 13:30-14:29 Txt (1716[44,54],1724[44,62]): KWONG ON Txt
		 * (1830[47,54],1833[47,57]): S02 Txt (1939[50,54],1950[50,65]): SQU
		 * Squash
		 */
		for (int i = 0; i < foundNodeList.size(); i++) {
			if (i <= 5 || i % 6 != 0 || i + 1 == foundNodeList.size()) {
				continue;
			}
			Booking booking = new Booking();

			Node dateNode = foundNodeList.elementAt(i).getNextSibling();
			Node timeNode = foundNodeList.elementAt(i + 1).getNextSibling();
			Node centreNode = foundNodeList.elementAt(i + 2).getNextSibling();
			Node courtNode = foundNodeList.elementAt(i + 3).getNextSibling();
			Node activityNode = foundNodeList.elementAt(i + 4).getNextSibling();

			booking.setDate(dateNode.getText());
			booking.setTime(timeNode.getText());
			booking.setCourt(courtNode.getText());
			booking.setCentre(centreNode.getText());
			booking.setCourt(courtNode.getText());
			booking.setActivity(activityNode.getText());

			Log.d(TAG, "Booking : " + booking.toString());
			bookingList.add(booking);
		}
		return bookingList;
	}

}
